package com.esyoga.yoyoga.service.impl;
import com.esyoga.yoyoga.mapper.TAttentionMapper;
import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.pojo.DTO.EachOtherAttentionDTO;
import com.esyoga.yoyoga.service.AttentionService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Data
@Service
public class AttentionServiceImpl implements AttentionService {
   @Resource
    TAttentionMapper tAttentionMapper;
   @Resource
    TAttentionExample tAttentionExample;
   @Resource
   TStudentExample tStudentExample;
   @Resource
   TStudentMapper tStudentMapper;
   @Resource
   TTrainerExample tTrainerExample;
   @Resource
   TTrainerMapper tTrainerMapper;
    //添加一个关注
    @Transactional
    @Override
    public String addOneAttention(Integer userid1, Integer usertype1, Integer userid2, Integer usertype2) {
        int row=0;
        String result="关注失败!";
        TAttention tAttention=new TAttention();
        tAttention.setaUserid1(userid1);
        tAttention.setaUserid2(userid2);
        tAttention.setaUsertype1(usertype1);
        tAttention.setaUsertype2(usertype2);
        row= tAttentionMapper.insertSelective(tAttention);
        if(row!=0){
            result="关注成功!";
        }
        return result;
    }
//取消关注
    @Transactional
    @Override
    public String delOneAttention(Integer userid1, Integer usertype1, Integer userid2, Integer usertype2) {
        int row=0;
        String result="取消关注失败!";
        tAttentionExample.clear();
        TAttentionExample.Criteria criteria=tAttentionExample.createCriteria();
        criteria.andAUserid1EqualTo(userid1);
        criteria.andAUserid2EqualTo(userid2);
        criteria.andAUsertype1EqualTo(usertype1);
        criteria.andAUsertype2EqualTo(usertype2);
        row=tAttentionMapper.deleteByExample(tAttentionExample);
        if(row!=0){
            result="取关成功!";
        }
        return result;
    }
//判断是否已关注
    @Override
    public String isAttention(Integer userid1, Integer usertype1, Integer userid2, Integer usertype2) {
        String result="已经关注对方了!";
        tAttentionExample.clear();
        TAttentionExample.Criteria criteria=tAttentionExample.createCriteria();
        criteria.andAUserid1EqualTo(userid1);
        criteria.andAUserid2EqualTo(userid2);
        criteria.andAUsertype1EqualTo(usertype1);
        criteria.andAUsertype2EqualTo(usertype2);
       List<TAttention> tAttentions= tAttentionMapper.selectByExample(tAttentionExample);
        TAttention tAttention= tAttentions.get(0);
        //如果查询不到，那么就是未关注
        if(tAttention==null){
            result="未关注对方!";
        }
        return result;
    }
//与当前用户互关的用户的类型和头像等
    @Override
    public List<EachOtherAttentionDTO> findEachOtherAttention(Integer userid1, Integer usertype1) {
        //存放要返回前端的所有与之互关的人
        List<EachOtherAttentionDTO> eachOtherAttentionDTOS=new ArrayList<EachOtherAttentionDTO>();
        tAttentionExample.clear();
        TAttentionExample.Criteria criteria=tAttentionExample.createCriteria();
        criteria.andAUserid1EqualTo(userid1);
        criteria.andAUsertype1EqualTo(usertype1);
        //获取到此用户关注的所有人
        List<TAttention> tAttentions=tAttentionMapper.selectByExample(tAttentionExample);
        //获取到关注的人的id集合
        List<Integer> userIdList = new ArrayList<Integer>();
        //循环加入
        for (TAttention tAttention : tAttentions
        ) {
            userIdList.add(tAttention.getaUserid2());
        }
        //将被关注人id作为条件
        tAttentionExample.clear();
        TAttentionExample.Criteria criteria2 = tAttentionExample.createCriteria();
        //查询被关注者作为主关注者在此集合里面的id和他的被关注者为当前用户的用户集合
        criteria2.andAUserid1In(userIdList);
        criteria2.andAUserid2EqualTo(userid1);
        //获得相互关注的对象
        List<TAttention> tAttentionList = tAttentionMapper.selectByExample(tAttentionExample);

        //取出互关的被关注者id
        for (TAttention ta:tAttentionList
        ) {
            //用于存放当前用户关注方的类型和基本信息
            EachOtherAttentionDTO eachOtherAttentionDTO=new EachOtherAttentionDTO();
            eachOtherAttentionDTO.setUserType(ta.getaUsertype1());
            //判断被关注者是什么类型，如果是学员：ta.getaUsertype1()为当前用户的被关注者作为主关注者的id、类型
            if(ta.getaUsertype1()==0){
                tStudentExample.clear();
                TStudentExample.Criteria criteria3=tStudentExample.createCriteria();
                criteria3.andSIdEqualTo(ta.getaUserid1());
                //状态为正常
                criteria3.andSStateEqualTo(0);
                List<TStudent> tStudents= tStudentMapper.selectByExample(tStudentExample);
                eachOtherAttentionDTO.setTStudents(tStudents);
            }else if(ta.getaUsertype1()==1){
                //用户类型为私教时
                tTrainerExample.clear();
                TTrainerExample.Criteria criteria7= tTrainerExample.createCriteria();
                criteria7.andTIdEqualTo(ta.getaUserid1());
                //状态不为禁用时
                criteria7.andTStateNotEqualTo(2);
                List<TTrainer> tTrainers=  tTrainerMapper.selectByExample(tTrainerExample);
                eachOtherAttentionDTO.setTTrainers(tTrainers);
            }
            //循环加入dto集合
            eachOtherAttentionDTOS.add(eachOtherAttentionDTO);

        }
        //System.out.println(eachOtherAttentionDTOS);
        return eachOtherAttentionDTOS;
    }
}
